/*************************************************************************
	> File Name: client.h
	> Author: xuwenlong
	> Mail: myxuan475@126.com 
	> Created Time: 2018年09月17日 星期一 15时02分16秒
 ************************************************************************/
#ifndef __ACQNET_USER_H__
#define __ACQNET_USER_H__
#include <iostream>
#include <sys/select.h>
#include <sys/socket.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <stdio.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string>
#include <string.h>
#include <event.h>
#include <stdlib.h>
#include <utils/threads/thread.h>
#include <utils/threads/thread_delegate.h>
#include <defines.h>
#include <transport/transport.h>
#include "modbus.h"

class AcqNet;
class AcqUser;
typedef utils::SharedPtr<AcqUser> tAcqUser;

class AcqUser:public Modbus, public utils::RefrenceObj,public threads::ThreadDelegate
{
public:
    AcqUser(AcqNet* server);
    virtual ~AcqUser();

    virtual void threadMain();
    bool startWork(int fd, sockaddr *addr);
    void stopWork();

    bool isValid() {return (m_pbase != NULL && m_bev != NULL);}
    virtual BOOL SendData(const char *data,S32 bytes);
    virtual BOOL IsTaskExit();

    static void DoRead(struct bufferevent *bev, void *ctx);
    static void DoError(struct bufferevent *bev, short ev, void *ctx);
    void CloseSession();

    AcqNet *server();

private:
    struct event_base*  m_pbase;
    struct bufferevent* m_bev;
    evutil_socket_t     m_ifd;

    threads::Thread    *m_pThread;
    Lock                m_ilock;
//    ConditionalVariable m_cond_;

protected:
    sockaddr             m_iaddr;
    AcqNet*              m_pServer;
};

#endif
